﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Project.Module.DAO.BL;
using Project.Module.DAO.IBL;
using Project.Module.Model;
using Project.Module.Model.CMS;
using Project.Engine.Utilities;
namespace Project.Module.DAO.CMSBL
{
    public class SupplierBL : IBLTable<Supplier, int>
    {
        public void AddNewSupplier(string name, string desc, string address, string user)
        {
            int id = GetMaxID() + 1;
            DateTime date = DateTime.Now;

            string sql = @"insert into Supplier (
                              SupplierID, SupplierName, Description, 
                              SupplierAddress, CreatedBy, CreatedDate
                           ) values (
                              {0}, {1}, {2}, {3}, {4}, {5}
                           )";
            var query = this.Context.ExecuteQuery<CustomerModel>(sql, new object[] { id, name, desc, address, user, date }).ToList();
        }

        public void EditSupplier(int id, string name, string desc, string address, string user)
        {
            DateTime date = DateTime.Now;

            string sql = @"update Supplier 
                           set SupplierName = {0}, Description = {1}, 
                               SupplierAddress = {2}, ModifiedBy = {3}, ModifiedDate = {4}
                           where SupplierID = {5}";
            var query = this.Context.ExecuteQuery<CustomerModel>(sql, new object[] { name, desc, address, user, date, id }).ToList();
        }

        private int GetMaxID()
        {
            int Id = 0;
            var query = from e in this.Context.Suppliers
                        orderby e.SupplierID descending
                        select new { Id = e.SupplierID };

            if (query != null)
            {
                Id = ConvertUtil.ToInt32(query.Select(c => c.Id).FirstOrDefault().ToString());
            }

            // return
            return Id;
        }

        public List<SupplierModel> GetAllSupplier()
        {
            var query = from e in this.Context.Suppliers
                        orderby e.CreatedDate descending
                        select new SupplierModel()
                        {
                            SupplierID = e.SupplierID,
                            SupplierName = e.SupplierName,
                            address = e.SupplierAddress,
                            description = e.Description,
                        };

            if (query != null)
            {
                return query.Take(50).ToList();
            }

            return null;
        }

        public SupplierModel GetDetailSupplier(int supplierID)
        {
            var query = from e in this.Context.Suppliers
                        where e.SupplierID == supplierID
                        orderby e.CreatedDate descending
                        select new SupplierModel()
                        {
                            SupplierID = e.SupplierID,
                            SupplierName = e.SupplierName,
                            address = e.SupplierAddress,
                            description = e.Description,
                        };

            if (query != null)
            {
                return query.ToList().FirstOrDefault();
            }

            return null;
        }

        public string GetSupplierByID(int supplierID)
        {
            var query = from e in this.Context.Suppliers
                        where e.SupplierID == supplierID
                        select new { name = e.SupplierName, };

            if (query != null)
            {
                return ConvertUtil.ToString(query.Select(c => c.name).FirstOrDefault());
            }

            return null; ;
        }

        public void DeleteSupplier(int supplierID)
        {
            Supplier supplier = this.Context.Suppliers.Single(c => c.SupplierID == supplierID);
            this.Context.Suppliers.DeleteOnSubmit(supplier);
            this.Context.SubmitChanges();
        }

        public List<SupplierModel> Search(string name, string address, string desc)
        {
            var query = from e in this.Context.Suppliers
                        where (name != String.Empty && e.SupplierName.Contains(name) == true) ||
                        (address != String.Empty && e.SupplierAddress.Contains(address) == true) ||
                        (desc != String.Empty && e.Description.Contains(desc) == true)
                        orderby e.CreatedDate descending
                        select new SupplierModel()
                        {
                            SupplierID = e.SupplierID,
                            SupplierName = e.SupplierName,
                            address = e.SupplierAddress,
                            description = e.Description,
                        };

            if (query != null)
            {
                return query.ToList();
            }

            return null;
        }

        public List<SupplierModel> SearchSupplier(string name)
        {
            var query = from e in this.Context.Suppliers
                        where e.SupplierName.Contains(name) == true
                        orderby e.CreatedDate descending
                        select new SupplierModel()
                        {
                            SupplierID = e.SupplierID,
                            SupplierName = e.SupplierName,
                            address = e.SupplierAddress,
                            description = e.Description,
                        };

            if (query != null)
            {
                return query.ToList();
            }

            return null;
        }

        public List<SupplierModel> SearchSupplierByAddress(string address)
        {
            var query = from e in this.Context.Suppliers
                        where e.SupplierAddress.Contains(address) == true
                        orderby e.CreatedDate descending
                        select new SupplierModel()
                        {
                            SupplierID = e.SupplierID,
                            SupplierName = e.SupplierName,
                            address = e.SupplierAddress,
                            description = e.Description,
                        };

            if (query != null)
            {
                return query.ToList();
            }

            return null;
        }

        public List<SupplierModel> SearchSupplierByDesc(string desc)
        {
            var query = from e in this.Context.Suppliers
                        where e.Description.Contains(desc) == true
                        orderby e.CreatedDate descending
                        select new SupplierModel()
                        {
                            SupplierID = e.SupplierID,
                            SupplierName = e.SupplierName,
                            address = e.SupplierAddress,
                            description = e.Description,
                        };

            if (query != null)
            {
                return query.ToList();
            }

            return null;
        }
    }
}
